# -*- coding:utf-8 -*-
# @Time: 2020/8/14 14:15
# @Author: liupengfei
# @File: jwt.py

import logging

from django.contrib.auth import get_user_model

from apps.account.serializers import UserSerializer
from apps.member.serializers import MemberSerializer,Member

User = get_user_model()

def jwt_response_payload_error_handler(serializer, request=None):
    #print("JWT: serializer=%s", serializer)
    return {
        'status': False,
        'message': "login failed",
        'detail': serializer.errors
    }

def jwt_response_payload_handler(token, user=None, request=None):
    #print("JWT: token=%s", token, user)
    key = 'user'

    if user.is_administrator():
        print('----------')
        value = UserSerializer(user, context={'request': request}).data
        value['is_staff'] = True
        value['is_administrator'] = True
        value['is_member'] = False
        value['is_authenticated'] = True
    else:
        member = Member.objects.filter(user=user).first()
        value = MemberSerializer(member, context={'request': request}).data
        value['is_staff'] = False
        value['is_administrator'] = False
        value['is_member'] = True
        value['is_authenticated'] = True

    return {
        'status': True,
        'token': token,
        key: value
    }
